package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.CollegeDetail;
import com.vehicle.bean.UserLog;
import com.vehicle.mapper.CollegeDetailMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.CollegeDetailService;
import com.vehicle.util.LogUtil;
import com.vehicle.vo.CollegeDetailListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-05-15
 */
@Service
public class CollegeDetailServiceImpl extends ServiceImpl<CollegeDetailMapper, CollegeDetail> implements CollegeDetailService {

    @Autowired
    CollegeDetailMapper collegeDetailMapper;

    @Autowired
    UserLogMapper userLogMapper;

    @Override
    public Boolean addCollegeDetail(String ip,Integer id,Integer collegeId, String detail) {
        CollegeDetail collegeDetail = new CollegeDetail();
        collegeDetail.setCollegeId(collegeId);
        collegeDetail.setDetail(detail);
        int b = collegeDetailMapper.insert(collegeDetail);
        if(b == 1){
            userLogMapper.insert(LogUtil.insertAdminInsert(id,"添加学院详情ID"+collegeId,ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminInsert(id,"添加学院详情ID"+collegeId,ip,"失败"));
        return false;
    }

    @Override
    public Integer getCollegeIdById(Integer cdId) {
        return collegeDetailMapper.getCollegeId(cdId);
    }

    @Override
    public String getDetailById(Integer cdId) {
        return collegeDetailMapper.getDetail(cdId);
    }

    @Override
    public CollegeDetailListVO getAllList(Integer page, Integer size) {
        Page<CollegeDetail> pageInfo = new Page<>();
        Page<CollegeDetail> collegeDetailPage = collegeDetailMapper.selectPage(pageInfo,null);
        CollegeDetailListVO vo = new CollegeDetailListVO();
        vo.setSize(collegeDetailPage.getSize());
        vo.setTotal(collegeDetailPage.getTotal());
        vo.setContent(collegeDetailPage.getRecords());
        return vo;
    }

    @Override
    public CollegeDetailListVO getListByCollegeId(Integer collegeId, Integer page, Integer size) {
        Page<CollegeDetail> pageInfo = new Page<>();
        QueryWrapper<CollegeDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("college_id",collegeId);
        Page<CollegeDetail> collegeDetailPage = collegeDetailMapper.selectPage(pageInfo,queryWrapper);
        CollegeDetailListVO vo = new CollegeDetailListVO();
        vo.setSize(collegeDetailPage.getSize());
        vo.setTotal(collegeDetailPage.getTotal());
        vo.setContent(collegeDetailPage.getRecords());
        return vo;
    }

    @Override
    public Boolean deleteCollegeDetail(String ip,Integer id,Integer cdId) {
        int b  = collegeDetailMapper.deleteById(cdId);
        if(b==1){
            userLogMapper.insert(LogUtil.insertAdminDelete(id,"删除学院详情ID"+cdId,ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminDelete(id,"删除学院详情ID"+cdId,ip,"失败"));
        return false;
    }

    @Override
    public Boolean updateCollegeDetailById(String ip,Integer id,Integer cdId, Integer collegeId, String detail) {
        UpdateWrapper<CollegeDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cd_id",cdId);
        updateWrapper.set("college_id",collegeId);
        updateWrapper.set("detail",detail);
        int b = collegeDetailMapper.update(null,updateWrapper);
        if(b==1){
            userLogMapper.insert(LogUtil.insertAdminUpdate(id,"修改学院详情ID"+cdId,ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminUpdate(id,"修改学院详情ID"+cdId,ip,"成功"));
        return false;
    }
}
